package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatGiftBox;
import com.happy.cat.project.system.service.ICatGiftBoxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 礼物惊喜盒子Controller
 *
 * @author verne
 * @date 2020-07-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/giftBox" )
@Api("礼物惊喜盒子 API")
public class CatGiftBoxController extends BaseController {

    private final ICatGiftBoxService iCatGiftBoxService;

    /**
     * 查询礼物惊喜盒子列表
     */
    @ApiOperation(value = "查询礼物惊喜盒子列表",notes = "查询礼物惊喜盒子列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:giftBox:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatGiftBox catGiftBox)
    {
        startPage();
        LambdaQueryWrapper<CatGiftBox> lqw = new LambdaQueryWrapper<CatGiftBox>();
        if (StringUtils.isNotBlank(catGiftBox.getBoxName())){
            lqw.like(CatGiftBox::getBoxName ,catGiftBox.getBoxName());
        }
        if (StringUtils.isNotBlank(catGiftBox.getBoxImg())){
            lqw.eq(CatGiftBox::getBoxImg ,catGiftBox.getBoxImg());
        }
        if (catGiftBox.getBoxMoney() != null){
            lqw.eq(CatGiftBox::getBoxMoney ,catGiftBox.getBoxMoney());
        }
        if (catGiftBox.getAmount() != null){
            lqw.eq(CatGiftBox::getAmount ,catGiftBox.getAmount());
        }
        if (StringUtils.isNotBlank(catGiftBox.getRemarks())){
            lqw.eq(CatGiftBox::getRemarks ,catGiftBox.getRemarks());
        }
        if (catGiftBox.getCreateDate() != null){
            lqw.eq(CatGiftBox::getCreateDate ,catGiftBox.getCreateDate());
        }
        if (catGiftBox.getStatus() != null){
            lqw.eq(CatGiftBox::getStatus ,catGiftBox.getStatus());
        }
        List<CatGiftBox> list = iCatGiftBoxService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出礼物惊喜盒子列表
     */
    @ApiOperation(value = "导出礼物惊喜盒子列表",notes = "导出礼物惊喜盒子列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:giftBox:export')" )
    @Log(title = "礼物惊喜盒子" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatGiftBox catGiftBox) {
        LambdaQueryWrapper<CatGiftBox> lqw = new LambdaQueryWrapper<CatGiftBox>(catGiftBox);
        List<CatGiftBox> list = iCatGiftBoxService.list(lqw);
        ExcelUtil<CatGiftBox> util = new ExcelUtil<CatGiftBox>(CatGiftBox. class);
        return util.exportExcel(list, "giftBox" );
    }

    /**
     * 获取礼物惊喜盒子详细信息
     */
    @ApiOperation(value = "获取礼物惊喜盒子详细信息",notes = "获取礼物惊喜盒子详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:giftBox:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {

        return AjaxResult.success(iCatGiftBoxService.getById(id));
    }

    /**
     * 新增礼物惊喜盒子
     */
    @ApiOperation(value = "新增礼物惊喜盒子",notes = "新增礼物惊喜盒子",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:giftBox:add')" )
    @Log(title = "礼物惊喜盒子" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatGiftBox catGiftBox) {
        catGiftBox.setCreateDate(new Date());
        catGiftBox.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatGiftBoxService.save(catGiftBox) ? 1 : 0);
    }

    /**
     * 修改礼物惊喜盒子
     */
    @ApiOperation(value = "修改礼物惊喜盒子",notes = "修改礼物惊喜盒子",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:giftBox:edit')" )
    @Log(title = "礼物惊喜盒子" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatGiftBox catGiftBox) {
        catGiftBox.setCreateDate(new Date());
        catGiftBox.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatGiftBoxService.updateById(catGiftBox) ? 1 : 0);
    }

    /**
     * 删除礼物惊喜盒子
     */
    @ApiOperation(value = "删除礼物惊喜盒子",notes = "删除礼物惊喜盒子",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:giftBox:remove')" )
    @Log(title = "礼物惊喜盒子" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatGiftBoxService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
